<?php
namespace app\api\controller;

use app\common\controller\ApiController;
use think\App;
use \app\admin\traits\Curd;
use think\facade\Db;
use app\api\controller\Adv;
use app\api\model\User as ModelUser;
use PhpOffice\PhpSpreadsheet\Reader\Xls\MD5;
use app\admin\controller\Sample;
use think\captcha\facade\Captcha;

class Shop extends ApiController
{
    protected $noNeedLogin = [
        "get_banner_list",
        "get_announcement_list",
        "recommend_service",
        "test",
        "get_protocol_kist",
        "listof_influencers_list",
        "gender_tags",
        "expert_detail",
        "dynamic_List",
        "dynamic_follow",
        "merchant_list",
        "upload",
        "getcaptcha"
        
        
    ];
    protected $mustLogin = [];
    protected $counter = 0;

    use \app\admin\traits\Curd;
    public function __construct(App $app)
    {
        parent::__construct($app);
    }
    //轮播图列表
    public function get_banner_list()
    {
        $param = $this->request->param();
        $num = isset($param['num'])?$param['num']:5;
        //type 1首页  2横幅
        if (empty($param['type'])) {
            $this->error('类型不能为空');
        }
        if($param['type'] == 1){
            $list = Db::name("banner")->where("type",$param["type"])->order('id desc')->paginate($num);
        }elseif($param['type'] == 2){
            $list = Db::name("banner")->where("type",$param["type"])->order('id desc')->paginate($num);
        }else{
            $this->error('类型不存在');
        }
        
  
        $this->success('获取成功', $list);
    }
 
    //新增职位
    public function addjob_position(){
        $param = $this->request->param();
        $rule = [
            'position_name|职位名称'      => 'require',
            'is_pricefixed|是否固定薪资'       => 'require',
            'salary_type|薪资单位'       => 'require',
            'job_description|工作描述'       => 'require',
            'greeting_message|打招呼语'       => 'require',
            'contact_message|获取联系方式后消息'       => 'require',
            'contact_type|联系方式'       => 'require'
        ];
        $this->validate($param, $rule);

        // $contact_phone = [
        //     [
        //         "phone_number"=>"13991984919",
        //         "number"=>10
        //     ],
        //     [
        //         "phone_number"=>"13991984918",
        //         "number"=>20
        //     ],
        // ];
        // $contact_qq = [
        //     [
        //         "qq_number"=>"123456",
        //         "qq_qr_code"=>"",
        //         "qqnumber"=>10
        //     ],
        //     [
        //         "qq_number"=>"456789",
        //         "qq_qr_code"=>"",
        //         "qqnumber"=>20
        //     ],
        // ];
        // $contact_wx = [
        //     [
        //         "wechat_number"=>"147258",
        //         "wechat_qr_code"=>"",
        //         "wxnumber"=>10
        //     ],
        //     [
        //         "wechat_number"=>"258369",
        //         "wechat_qr_code"=>"",
        //         "wxnumber"=>20
        //     ],
        // ];

 

        //联系方式  contact_type 1手机 2qq 3微信
        // if($param["contact_type"] == 1){
        //     if(empty($param["contact_phone"])){
        //         $this->error("手机参数不能为空");
        //     }
        // }elseif($param["contact_type"] == 2){
        //     if(empty($param["contact_qq"])){
        //         $this->error("qq参数不能为空");
        //     }
        // }elseif($param["contact_type"] == 3){
        //     if(empty($param["contact_wx"])){
        //         $this->error("微信参数不能为空");
        //     }
        // }else{
        //     $this->error("类型不存在");
        // }
       // $contact_phone = $param["contact_phone"];
        // $contact_qq = $param["contact_qq"];
        // $contact_wx = $param["contact_wx"];
        $contact_phone = [
            [
                "phone_number"=>"13991984919",
                "number"=>10
            ],
            [
                "phone_number"=>"13991984918",
                "number"=>20
            ],
        ];
        $contact_qq = [];
        $contact_wx = [];
        //isit_nationwide  是否全国  1否 2是
        if($param["isit_nationwide"] == 1){
            if(empty($param["region_province"])){
                $this->error("省不能为空");
            }
            if(empty($param["region_city"])){
                $this->error("市不能为空");
            }
            if(empty($param["region_region"])){
                $this->error("区不能为空");
            }
        }
        //是否固定薪资  is_pricefixed 1否，2是
        if($param["is_pricefixed"] == 2){
            if(empty($param["salary"])){
                $this->error("薪资不能为空");
            }
        }elseif($param["is_pricefixed"] == 1){
            if(empty($param["salary_max"])){
                $this->error("最大薪资不能为空");
            }
            if(empty($param["salary_min"])){
                $this->error("最小薪资不能为空");
            }
        }else{
            $this->error("工资类型不存在");
        }
        $salary = $param["salary"];
        $salary_max = $param["salary_max"];
        $salary_min = $param["salary_min"];
        //招聘开始日期
        if(empty($param["selection_date_start"])){$selection_date_start = NULL;}else{$selection_date_start = $param["selection_date_start"];}
        //招聘结束日期
        if(empty($param["selection_date_end"])){$selection_date_end = NULL;}else{$selection_date_end = $param["selection_date_end"];}
        //招聘开始时间
        if(empty($param["work_time_start"])){$work_time_start = NULL;}else{$work_time_start = $param["work_time_start"];}
        //招聘结束时间
        if(empty($param["work_time_end"])){$work_time_end = NULL;}else{$work_time_end = $param["work_time_end"];}

        $data = [
            "company_id"=>$this->uid,
            "position_name"=>$param["position_name"],
            "is_pricefixed"=>$param["is_pricefixed"],
            "salary"=>$salary,
            "salary_max"=>$salary_max,
            "salary_min"=>$salary_min,
            "salary_type"=>$param["salary_type"],
            "region_province"=>$param["region_province"],
            "region_city"=>$param["region_city"],
            "region_region"=>$param["region_region"],
            "job_description"=>$param["job_description"],
            "selection_date_start"=>$selection_date_start,
            "selection_date_end"=>$selection_date_end,
            "work_time_start"=>$work_time_start,
            "work_time_end"=>$work_time_end,
            "contact_message"=>$param["contact_message"],
            "greeting_message"=>$param["greeting_message"],
            "contact_type"=>$param["contact_type"],
            "contact_genre"=>$param["contact_genre"],
            "created_at"=>date("Y-m-d H:i:s",time()),
            "cur_status"=>3
        ];
        Db::connect('tmp')->table('job_position')->insert($data);
        $job_position_id = Db::connect('tmp')->table('job_position')->getLastInsID();
         //判断联系类型 1手机 2QQ 3微信
         if($param["contact_type"] == 1){
            foreach($contact_phone as $k=>$v){
                $v["job_position_id"] = $job_position_id;
                $v["created_at"]= date("Y-m-d H:i:s",time());
                $contact_phone[$k]=$v;
            }
            Db::connect('tmp')->table('job_position_contact_phone')->insertAll($contact_phone);
            
        }elseif($param["contact_type"] == 2){
            foreach($contact_qq as $k=>$v){
                $v["job_position_id"] = $job_position_id;
                $v["created_at"]= date("Y-m-d H:i:s",time());
                $contact_qq[$k]=$v;
            }
            Db::connect('tmp')->table('job_position_contact_qq')->insertAll($contact_qq);
        }elseif($param["contact_type"] == 3){
            foreach($contact_wx as $k=>$v){
                $v["job_position_id"] = $job_position_id;
                $v["created_at"]= date("Y-m-d H:i:s",time());
                $contact_wx[$k]=$v;
            }
            Db::connect('tmp')->table('job_position_contact_wechat')->insert($contact_wx);
        }else{
            $this->error("类型不存在");
        }
        $this->success('保存成功');
    }


        //编辑职位
        public function editjob_position(){
            $param = $this->request->param();
            $rule = [
                'id|职位id'      => 'require',
                'position_name|职位名称'      => 'require',
                'is_pricefixed|是否固定薪资'       => 'require',
                'salary_type|薪资单位'       => 'require',
                'job_description|工作描述'       => 'require',
                'greeting_message|打招呼语'       => 'require',
                'contact_message|获取联系方式后消息'       => 'require',
                'contact_type|联系方式'       => 'require'
            ];
            $this->validate($param, $rule);
    
            // $contact_phone = [
            //     [
            //         "phone_number"=>"13991984919",
            //         "number"=>10
            //     ],
            //     [
            //         "phone_number"=>"13991984918",
            //         "number"=>20
            //     ],
            // ];
            // $contact_qq = [
            //     [
            //         "qq_number"=>"123456",
            //         "qq_qr_code"=>"",
            //         "qqnumber"=>10
            //     ],
            //     [
            //         "qq_number"=>"456789",
            //         "qq_qr_code"=>"",
            //         "qqnumber"=>20
            //     ],
            // ];
            // $contact_wx = [
            //     [
            //         "wechat_number"=>"147258",
            //         "wechat_qr_code"=>"",
            //         "wxnumber"=>10
            //     ],
            //     [
            //         "wechat_number"=>"258369",
            //         "wechat_qr_code"=>"",
            //         "wxnumber"=>20
            //     ],
            // ];
    
     
    
            //联系方式  contact_type 1手机 2qq 3微信
            // if($param["contact_type"] == 1){
            //     if(empty($param["contact_phone"])){
            //         $this->error("手机参数不能为空");
            //     }
            // }elseif($param["contact_type"] == 2){
            //     if(empty($param["contact_qq"])){
            //         $this->error("qq参数不能为空");
            //     }
            // }elseif($param["contact_type"] == 3){
            //     if(empty($param["contact_wx"])){
            //         $this->error("微信参数不能为空");
            //     }
            // }else{
            //     $this->error("类型不存在");
            // }
           // $contact_phone = $param["contact_phone"];
            // $contact_qq = $param["contact_qq"];
            // $contact_wx = $param["contact_wx"];
            $contact_phone = [
                [
                    "phone_number"=>"13991984919",
                    "number"=>10
                ],
                [
                    "phone_number"=>"13991984918",
                    "number"=>20
                ],
            ];
            $contact_qq = [];
            $contact_wx = [];
            //isit_nationwide  是否全国  1否 2是
            if($param["isit_nationwide"] == 1){
                if(empty($param["region_province"])){
                    $this->error("省不能为空");
                }
                if(empty($param["region_city"])){
                    $this->error("市不能为空");
                }
                if(empty($param["region_region"])){
                    $this->error("区不能为空");
                }
            }
            //是否固定薪资  is_pricefixed 1否，2是
            if($param["is_pricefixed"] == 2){
                if(empty($param["salary"])){
                    $this->error("薪资不能为空");
                }
            }elseif($param["is_pricefixed"] == 1){
                if(empty($param["salary_max"])){
                    $this->error("最大薪资不能为空");
                }
                if(empty($param["salary_min"])){
                    $this->error("最小薪资不能为空");
                }
            }else{
                $this->error("工资类型不存在");
            }
            $salary = $param["salary"];
            $salary_max = $param["salary_max"];
            $salary_min = $param["salary_min"];
            //招聘开始日期
            if(empty($param["selection_date_start"])){$selection_date_start = NULL;}else{$selection_date_start = $param["selection_date_start"];}
            //招聘结束日期
            if(empty($param["selection_date_end"])){$selection_date_end = NULL;}else{$selection_date_end = $param["selection_date_end"];}
            //招聘开始时间
            if(empty($param["work_time_start"])){$work_time_start = NULL;}else{$work_time_start = $param["work_time_start"];}
            //招聘结束时间
            if(empty($param["work_time_end"])){$work_time_end = NULL;}else{$work_time_end = $param["work_time_end"];}
    
            $data = [
                "company_id"=>$this->uid,
                "position_name"=>$param["position_name"],
                "is_pricefixed"=>$param["is_pricefixed"],
                "salary"=>$salary,
                "salary_max"=>$salary_max,
                "salary_min"=>$salary_min,
                "salary_type"=>$param["salary_type"],
                "region_province"=>$param["region_province"],
                "region_city"=>$param["region_city"],
                "region_region"=>$param["region_region"],
                "job_description"=>$param["job_description"],
                "selection_date_start"=>$selection_date_start,
                "selection_date_end"=>$selection_date_end,
                "work_time_start"=>$work_time_start,
                "work_time_end"=>$work_time_end,
                "contact_message"=>$param["contact_message"],
                "greeting_message"=>$param["greeting_message"],
                "contact_type"=>$param["contact_type"],
                "contact_genre"=>$param["contact_genre"],
                "updated_at"=>date("Y-m-d H:i:s",time()),
                "cur_status"=>3
            ];
            Db::connect('tmp')->table('job_position')->where("id",$param["id"])->update($data);
             //判断联系类型 1手机 2QQ 3微信
             if($param["contact_type"] == 1){
                foreach($contact_phone as $k=>$v){
                    $data_phone = [
                        "phone_number"=>$v["phone_number"],
                        "number"=>$v["number"],
                        "job_position_id"=>$param["id"],
                        "updated_at"=>date("Y-m-d H:i:s",time())
                    ];
                    Db::connect('tmp')->table('job_position_contact_phone')->where("job_position_id",$param["id"])->update($data_phone);
                }
            }elseif($param["contact_type"] == 2){
                foreach($contact_qq as $k=>$v){
                    $data_qq = [
                        "qq_number"=>$param["qq_number"],
                        "qq_qr_code"=>$param["qq_qr_code"],
                        "qqnumber"=>$param["number"],
                        "job_position_id"=>$param["id"],
                        "updated_at"=>date("Y-m-d H:i:s",time())
                    ];
                    Db::connect('tmp')->table('job_position_contact_qq')->where("job_position_id",$param["id"])->update($data_qq);
                }
                
            }elseif($param["contact_type"] == 3){
                foreach($contact_wx as $k=>$v){
                    $data_wx = [
                        "wechat_number"=>$param["wechat_number"],
                        "wechat_qr_code"=>$param["wechat_qr_code"],
                        "wxnumber"=>$param["wxnumber"],
                        "job_position_id"=>$param["id"],
                        "updated_at"=>date("Y-m-d H:i:s",time())
                    ];
                    Db::connect('tmp')->table('job_position_contact_wechat')->where("job_position_id",$param["id"])->update($data_wx);
                }
              
            }else{
                $this->error("类型不存在");
            }
            $this->success('保存成功');
        }
      
    //职位列表  cur_status 1上架  2下架  3待审核  4拒绝
    public function putonshelve_positionlist(){
        $param = $this->request->param();
        if(empty($param["type"])){
            $this->error("类型不能为空");
        }
        $where = [['company_id', '=', $this->uid]];
        $num = isset($param['num'])?$param['num']:10;
        if(!empty($param["isit_nationwide"]) && $param["isit_nationwide"] == 2){
            $where = [
                ['region_province', '=', ""],
                ['region_city', '=', ""],
                ['region_region', '=', ""],
            ];
        }elseif(!empty($param["isit_nationwide"]) && $param["isit_nationwide"] == 1){
            $where = [
                ['region_province', '=', $param["region_province"]],
                ['region_city', '=', $param["region_city"]],
                ['region_region', '=', $param["region_region"]],
            ];
        }
        if(!empty($param["position_name"])){
            $where = [
                ['position_name', 'like', "%{$param['position_name']}%"],
            ];
        }
        //报名人数 bmrs  浏览人数 llrs  提交时间 tjsj  更新时间 gxsj   // $sort = ['id' => 'desc'];
        if(!empty($param["bmrs"])){
            $sort = ['jp.recruit_num' => 'desc'];
        }elseif(!empty($param["llrs"])){
            $sort = ['ps.uv' => 'desc'];
        }elseif(!empty($param["tjsj"])){
            $sort = ['jp.created_at' => 'desc'];
        }elseif(!empty($param["gxsj"])){
            $sort = ['jp.updated_at' => 'desc'];
        }else{
            $sort = ['jp.id' => 'desc'];
        }

        $list = Db::connect('tmp')->table('job_position')
        ->alias("jp")
        ->join("job_position_statistics ps","ps.job_position_id = jp.id")
        ->where("jp.cur_status",$param["type"])
        ->where($where)
        ->where("jp.deleted_at",NULL)
        ->order($sort)
        ->field(
            "jp.id,jp.position_name,jp.region_province,
            jp.region_city,jp.region_region,jp.recruit_num,
            jp.selection_date_start,jp.selection_date_end,jp.work_time_start,
            jp.work_time_end,jp.created_at,jp.updated_at,jp.cur_status,
            ps.uv"
        )
        ->paginate($num)->each(function($item){
            //浏览量和浏览人数
            // $job_position_statistics = Db::connect('tmp')->table('job_position_statistics')->where("job_position_id",$item["id"])->find();
            // if(!empty($job_position_statistics)){
            //     $item["pv"] = $job_position_statistics["pv"];
            //    // $v["uv"] = $job_position_statistics["uv"];
            // }else{
            //     $item["pv"] = 0;
            //    // $v["uv"] = 0;
            // }
            //招聘区域
            $region_province = Db::connect('tmp')->table('area')->where("code",$item["region_province"])->find();
            $region_city = Db::connect('tmp')->table('area')->where("code",$item["region_city"])->find();
            $region_region = Db::connect('tmp')->table('area')->where("code",$item["region_region"])->find();
            if(!empty($region_province)){
                $region_province = $region_province["name"];
            }else{
                $region_province = "";
            }
            if(!empty($region_city)){
                $region_city = $region_city["name"];
            }else{
                $region_city = "";
            }
            if(!empty($region_region)){
                $region_region = $region_region["name"];
            }else{
                $region_region = "";
            }
            $recruitment_area = $region_province.$region_city.$region_region;
            if(!empty($recruitment_area)){
                $item["recruitment_area"] = $recruitment_area;
            }else{
                $item["recruitment_area"] = "全国";
            }

            return $item;
        });
        $this->success('获取成功', $list);
    }

    //报名列表
    public function registration_list(){
        $param = $this->request->param();
        $num = isset($param['num'])?$param['num']:5;
        $where = [['company_id', '=', $this->uid]];
        if(!empty($param["job_position_id"])){
            $where = [
                ['job_position_id', '=', $param['job_position_id']],
            ];
        }
        if(!empty($param["contact_type"])){
            $where = [
                ['contact_type', '=', $param['contact_type']],
            ];
        }
        $list = Db::connect('tmp')->table('sign_up_job_position')
        ->where($where)
        ->order("created_at desc")
        ->field("id,created_at,user_id,job_position_id,position_name,contact_type")
        ->paginate($num)->each(function($item){
              //联系类型  1手机  2QQ  3微信
              if($item["contact_type"] == 1){
                $job_position_contact_phone = Db::connect('tmp')->table('job_position_contact_phone')->where("job_position_id",$item["job_position_id"])->find();
                if(!empty($job_position_contact_phone)){
                    $item["registration_method"] = $job_position_contact_phone["phone_number"];
                    $item["qrcode"] = "";
                }else{
                    $item["registration_method"] = "";
                    $item["qrcode"] = "";
                }
            }elseif($item["contact_type"] == 2){
                $job_position_contact_qq = Db::connect('tmp')->table('job_position_contact_qq')->where("job_position_id",$item["job_position_id"])->find();
                if(!empty($job_position_contact_qq)){
                    $item["registration_method"] = $job_position_contact_qq["qq_number"];
                    $item["qrcode"] = $job_position_contact_qq["qq_qr_code"];
                }else{
                    $item["registration_method"] = "";
                    $item["qrcode"] = "";
                }
            }elseif($item["contact_type"] == 3){
                $job_position_contact_wechat = Db::connect('tmp')->table('job_position_contact_wechat')->where("job_position_id",$item["job_position_id"])->find();
                if(!empty($job_position_contact_wechat)){
                    $item["registration_method"] = $job_position_contact_wechat["wechat_number"];
                    $item["qrcode"] = $job_position_contact_wechat["wechat_qr_code"];
                }else{
                    $item["registration_method"] = "";
                    $vitem["qrcode"] = "";
                }
            }
            $user = Db::connect('tmp')->table('user')->where("id",$item["user_id"])->find();
            if(!empty($user["name"])){
                $item["name"] = $user["name"];
            }else{
                $item["name"] = "";
            }
            if(!empty($user["gender"])){
                $item["gender"] = $user["gender"];
            }else{
                $item["gender"] = "";
            }
            if(!empty($user["mobile"])){
                $item["mobile"] = $this->mask_phone($user["mobile"]);
            }else{
                $item["mobile"] = "";
            }
            $item["age"] = $this->birthday($user["birthday"]);
            return $item;
        });
        $this->success('获取成功', $list);
    }
    //根据年月日计算年龄
    public function birthday($birthday){
        if (!empty($birthday)){
            list($year,$month,$day)=explode("-", $birthday);
            $year_diff = date("Y") - $year;
            $month_diff = date("m") - $month;
            $day_diff = date("d") - $day;
            if ($day_diff < 0 || $month_diff < 0)
                $year_diff--;
            return $year_diff;
        }
        return 0;
    }
    //手机号加星
    function mask_phone($phone) {
        return substr_replace($phone, '****', 3, 4);
    }

    //已报名职位
    public function registered_positions(){
        $list = Db::connect('tmp')->table('sign_up_job_position')
        ->where("company_id",$this->uid)
        ->group("job_position_id")
        ->field("job_position_id")
        ->paginate(100)->each(function($item){
            $job_position = Db::connect('tmp')->table('job_position')->where("id",$item["job_position_id"])->find();
            $item["position_name"] = $job_position["position_name"];
            return $item;
        });
        $this->success('获取成功', $list);
    }

    //上传图片
    public function upload()
    {
        $file = request()->file('file');
        if (empty($file)) {
            $this->error('请选择上传文件');
        }
        try {
            validate('app\api\validate\Upload')
                ->scene('img')
                ->check(['file' => $file]);
            $savename = \think\facade\Filesystem::disk('public')->putFile('uploads', $file);
            $url = request()->domain() . '/' . $savename;
            $this->success('上传成功',$url);
        } catch (\think\exception\ValidateException $e) {
            $this->error('上传失败',$e->getMessage());
        }
    }
    
    //企业认证
    public function enterprise_certification(){
        $param = $this->request->param();
        //cur_type 1个人  2企业
        if(empty($param["cur_type"])){
            $this->error("类型不能为空");
        }
        if($param["cur_type"] == 1){
            $rule = [
                'company_name|真实姓名'      => 'require',
                'identity_card_front|身份证正面'       => 'require',
                'identity_card_back|身份证反面'       => 'require',
                'contact_person_name|联系人姓名'       => 'require|2',
                'cur_type|账号类型'       => 'require'
            ];
            $this->validate($param, $rule);
            $data = [
                "company_name"=>$param["company_name"],
                "identity_card_front"=>$param["identity_card_front"],
                "identity_card_back"=>$param["identity_card_back"],
                "contact_person_avatar"=>$param["contact_person_avatar"],
                "contact_person_name"=>$param["contact_person_name"],
                //"mobile"=>$param["mobile"],
                "cur_type"=>$param["cur_type"],
                "updated_at"=>date("Y-m-d H:i:s",time())
            ];
        }elseif($param["cur_type"] == 2){
            $rule = [
                'company_name|公司名称'      => 'require',
                'business_license_photo|营业执照照片'       => 'require',
                'contact_person_name|招聘联系人姓名'       => 'require|max:20',
                'cur_type|账号类型'       => 'require'
            ];
            $this->validate($param, $rule);
            $data = [
                "company_name"=>$param["company_name"],
                "business_license_photo"=>$param["business_license_photo"],
                "company_logo"=>$param["company_logo"],
                "contact_person_avatar"=>$param["contact_person_avatar"],
                "contact_person_name"=>$param["contact_person_name"],
               //"mobile"=>$param["mobile"],
                "cur_type"=>$param["cur_type"],
                "updated_at"=>date("Y-m-d H:i:s",time())
            ];
        }else{
            $this->error("类型不存在");
        }
        Db::connect('tmp')->table('company')->where("id",$this->uid)->update($data);
        $this->success('提交成功');
    }

    //修改手机号
    public function editphone(){
        $param = $this->request->param();
        $rule = [
            'old_phone|原手机号'      => 'require|mobile',
            'new_phone|新手机号'       => 'require|mobile',
            'code|验证码'       => 'require',
        ];
        $this->validate($param, $rule);
        $company = Db::connect('tmp')->table('company')->where("mobile",$param["old_phone"])->find();
        if(empty($company)){
            $this->error("手机号不存在");
        }
        $flag = $this->verifyCode($param["code"],$param["phone"],2);
        if(!$flag){
            $this->error('验证码错误或已过期');
        }
        Db::connect('tmp')->table('company')->where("id",$company["id"])->update(["mobile"=>$param["new_phone"]]);
        $this->success('修改成功');
    }


     //修改手机号发送短信验证码
     public function editphone_smsSend(){
        $param = $this->request->param();
        $rule = [
            'phone|手机号'      => 'require'
        ];
        $this->validate($param, $rule);
        $param['sms_type'] = 2;
        $count = Db::connect('tmp')->table('sms')->where('phone', $param['phone'])->where('status', 2)->whereDay('ctime')->count();
        if ($count >= 5) {
            $this->error('今日发送短信已达上限');
        }
        $code = rand(10000, 99999);
        $smsData = Db::connect('tmp')->table('sms')->where('phone', $param['phone'])->where('sms_type', $param['sms_type'])->where('delete_time',0)->find();
        if ($smsData) {
            Db::connect('tmp')->table('sms')->where('id', $smsData['id'])->update(['delete_time' => time()]);
        }
        $smsdata = ['phone' => $param['phone'], 'code' => $code, 'sms_type' => $param['sms_type'], 'expire_time' => strtotime('+ 5 minute'), 'ctime' => time()];
        $rst = Db::connect('tmp')->table('sms')->insert($smsdata);
        if ($rst) {
            $smsId = Db::connect('tmp')->table('sms')->getLastInsID();
            $ret = $this->newSms($param['phone'], $code);
            if ($ret['code'] == 1) {
                Db::connect('tmp')->table('sms')->where('id', $smsId)->update(['status' => 2,"error_msg"=>$ret["msg"]]);
                $this->success('验证码发送成功');
            } else {
                Db::connect('tmp')->table('sms')->where('id', $smsId)->update(['status' => 3,"error_msg"=>$ret["msg"]]);
                $this->error('验证码发送失败');
            }
        } else {
            $this->error('验证码发送失败');
        }
    }

    /**
     * 校验验证码
    */
    public function verifyCode($code, $phone, $sms_type)
    {
        $smsData = Db::connect('tmp')->table('sms')
            ->where('phone', $phone)
            ->where('sms_type', $sms_type)
            ->where('code', $code)
            ->where('delete_time', 0)
            ->where('expire_time', '>', time())
            ->find();
        if (empty($smsData)) {
            return false;
        } else {
            return true;
        }
    }

     /**
     * 获取验证码
     * @return [type] [description]
     */
    public function Newsms($phone, $code)
    {
        $args = [
            "phoneNumbers" => $phone,
            "signName" => "子米无限",
            "templateCode" => 'SMS_470525124',
            "templateParam" => "{\"code\":\"" . $code . "\"}"
        ];
        $result = Sample::main($args);
        $result = json_decode($result, true);
        if ($result['body']['Code'] == 'OK') {
            return ['code' => 1,"msg"=>$result['body']['Message']];
        } else {
            return ['code' => 0,"msg"=>$result['body']['Message']];
        }
    }
    
    //修改密码
    public function editpassword(){
        $param = $this->request->param();
        $rule = [
            'old_password|原密码'      => 'require',
            'new_password|新密码'      => 'require|min:8|max:20',
        ];
        $this->validate($param, $rule);
        $company = Db::connect('tmp')->table('company')->where("id",$this->uid)->find();
        if (empty($company)) {
            $this->error('商户不存在');
        }
        if (password($param['old_password']) != $company["password"]) {
            $this->error('原密码输入有误');
        }
        Db::connect('tmp')->table('company')->where("id",$this->uid)->update(["password"=>password($param['new_password'])]);
        $this->success('修改成功');

    }

    //生成图形验证码
    public function getcaptcha(){
        ob_clean();
        return Captcha::create();
    }
    
    //修改用户名
    public function change_username(){
        $param = $this->request->param();
        $rule = [
            'company_name|名称'      => 'require'
        ];
        $rule['code|验证码'] = 'require|captcha';
        $this->validate($rule,['code|验证码'=>'require|captcha']);
        Db::connect('tmp')->table('company')->where("id",$this->uid)->update(["company_name"=>$param["company_name"]]);
        $this->success('修改成功');
    }

    //上架状态的职位上线
    public function listing_position_online(){
        $param = $this->request->param();
        $rule = [
            'position_id|职位id'      => 'require'
        ];
        $this->validate($param, $rule);
        $position_id = explode(",",$param["position_id"]);
        foreach($position_id as $k=>$v){
            $flag = Db::connect('tmp')->table('job_position')->where("company_id",$this->uid)->where("id",$v)->find();
            if(empty($flag)){
                $this->error('职位不存在');
            }
            //cur_status  1上架 2下架
            if($flag["cur_status"] != 1 || $flag["position_upanddown"] == 1){
                $this->error('此状态不能上线');
            }
            Db::connect('tmp')->table('job_position')->where("company_id",$this->uid)->where("id",$v)->update(["position_upanddown"=>1]);
        }
        $this->success('修改成功');
    }

    //上架状态的职位下线
    public function listing_position_offline(){
        $param = $this->request->param();
        $rule = [
            'position_id|职位id'      => 'require'
        ];
        $this->validate($param, $rule);
        $position_id = explode(",",$param["position_id"]);
        foreach($position_id as $k=>$v){
            $flag = Db::connect('tmp')->table('job_position')->where("company_id",$this->uid)->where("id",$v)->find();
            if(empty($flag)){
                $this->error('职位不存在');
            }
            //cur_status  1上架 2下架
            if($flag["cur_status"] != 1 || $flag["position_upanddown"] == 2){
                $this->error('此状态不能下线');
            }
            //是否自动上线   is_automatic_online  1否，2是
            Db::connect('tmp')->table('job_position')->where("company_id",$this->uid)->where("id",$v)->update(["position_upanddown"=>2,"is_automatic_online"=>$param["is_automatic_online"]]);
        }
       
        $this->success('修改成功');
    }


















     

     

    
 
   
}
